package com.exercise.rpc.parse;/**
 * Created by kete on 2018-12-05.
 */

import com.exercise.rpc.common.model.RpcServiceBean;
import com.exercise.rpc.parse.spring.ReferenceBean;
import org.springframework.beans.factory.config.BeanDefinition;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * 作者：kete
 * 创建时间：2018-12-05 16:55
 * 功能描述：
 * 版本：
 */
public class MarioServiceBeanDefinitionParser extends AbstractBeanDefinitionParser {

    private final Class<?> beanClass;

    public MarioServiceBeanDefinitionParser(Class<?> beanClass) {
        this.beanClass = beanClass;
    }


    public AbstractBeanDefinition parseServiceBean(Element element){
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RpcServiceBean.class);

        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }

        String ref = element.getAttribute("ref");
        builder.addPropertyValue("ref", ref);

        String interfaceName = element.getAttribute("interface");
        builder.addPropertyValue("interfaceName", interfaceName);

        String group = element.getAttribute("group");
        if (StringUtils.hasText(group)) {
            builder.addPropertyValue("group", group);
        }

        String registry = element.getAttribute("registry");
        if (StringUtils.hasText(registry)) {
            builder.addPropertyValue("registry", registry);
        }

        String version = element.getAttribute("version");
        if (StringUtils.hasText(version)) {
            builder.addPropertyValue("version", version);
        }

        String timeout = element.getAttribute("timeout");
        if (StringUtils.hasText(timeout)) {
            builder.addPropertyValue("timeout", Integer.parseInt(timeout));
        }

        String retries = element.getAttribute("retries");
        if (StringUtils.hasText(retries)) {
            builder.addPropertyValue("retries", Integer.parseInt(retries));
        }

        String async = element.getAttribute("async");
        if (StringUtils.hasText(async)) {
            builder.addPropertyValue("async", Boolean.valueOf(async));
        }

        return builder.getBeanDefinition();
    }

    public AbstractBeanDefinition parseRef(Element element){
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ReferenceBean.class);

        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }

        String interfaceName = element.getAttribute("interface");
        builder.addPropertyValue("interfaceName", interfaceName);

        return builder.getBeanDefinition();
    }

    @Override
    protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {

        if(RpcServiceBean.class.equals(beanClass)){
            return parseServiceBean(element);
        }else if(ReferenceBean.class.equals(beanClass)){
            return parseRef(element);
        }else {
            return null;
        }


    }
}
